package controller;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.*;
import javax.servlet.http.*;

  /**
   *
   * @author atikahSH
   *
   */

public class Account extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, Exception {
        
        String page = "home";

        if (request.getParameter("page") != null) {
            page = request.getParameter("page");
        }

        RequestDispatcher dis = null;
        HttpSession session = request.getSession();
        
        if (page != null) {
            /* memanggil fungsi signUp dari kelas AccountLogic
             */
            if (page.equals("registered")) {
                session.removeAttribute("error");
                AccountLogic al = new AccountLogic(request);
                dis = request.getRequestDispatcher(al.signUp());
            } /* redirect ke halaman register
             */ else if (page.equals("register")) {
                session.removeAttribute("error");
                session.removeAttribute("tempReg");
                dis = request.getRequestDispatcher("register.jsp");
            } /* mamanggil fungsi home untuk kembali ke halaman utama (page awal)
             */ else if (page.equals("home")) {
                session.removeAttribute("error");
                AccountLogic al = new AccountLogic(request);
                dis = request.getRequestDispatcher(al.home());
            } /* mamanggil fungsi daftarUser dan melakukan fungsi hapus jika id terdeteksi
             */ else if (page.equals("list")) {
                session.removeAttribute("error");
                AccountLogic la = new AccountLogic(request);
                dis = request.getRequestDispatcher(la.hapusApoteker());
            } /* mamanggil fungsi untuk mengubah atribut yang terdapat dalam User
             */ else if (page.equals("edit")) {
                session.removeAttribute("error");
                AccountLogic la = new AccountLogic(request);
                dis = request.getRequestDispatcher(la.ubahProfil());
            } /* mamanggil fungsi untuk mengubah Profil dan menyimpan perubahan 
             * yang telah dilakukan
             */ else if (page.equals("edited")) {
                session.removeAttribute("error");
                AccountLogic la = new AccountLogic(request);
                dis = request.getRequestDispatcher(la.simpanUbahProfil());
            } /* mamanggil fungsi untuk  login
             */ else if (page.equals("loggedin")) {
                session.removeAttribute("error");
                AccountLogic la = new AccountLogic(request);
                dis = request.getRequestDispatcher(la.loggedIn());
            } /* mamanggil fungsi untuk melihat profil user
             */ else if (page.equals("profile")) {
                session.removeAttribute("error");
                AccountLogic la = new AccountLogic(request);
                dis = request.getRequestDispatcher(la.lihatProfil());
            } /* mamanggil fungsi untuk menampilkan profile promotor yang bisa dilihat oleh umum
             */ else if (page.equals("profile_public")) {
                session.removeAttribute("error");
                AccountLogic la = new AccountLogic(request);
                dis = request.getRequestDispatcher(la.lihatProfilPublic());
            } /* mamanggil fungsi untuk logout
             */ else if (page.equals("logout")) {
                session.removeAttribute("error");
                AccountLogic la = new AccountLogic(request);
                dis = request.getRequestDispatcher(la.logout());
            }
        } /* ketika page bernilai null maka akan dipanggil fungsi untuk redirect ke home
         */ else {
            session.removeAttribute("error");
            AccountLogic al = new AccountLogic(request);
            dis = request.getRequestDispatcher(al.home());
        }
        dis.forward(request, response);
    }     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception ex) {
            Logger.getLogger(Account.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception ex) {
            Logger.getLogger(Account.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
